<template>
	<view>
		<uni-section title="我的邀请" type="line"></uni-section>
		<view>
			<uni-list>
				<uni-list-item v-for="item in inviteList"
				:key="item.id" 
				:title="item.dry_invitor == user.id ? item.dry_accept_nickname : item.dry_invite_nickname"
				:note="(item.dry_invite_nickname == user.ext_nick_name ? '我' : item.dry_invite_nickname) + ' 邀请 ' + (item.dry_accept_nickname == user.ext_nick_name ? '我' : item.dry_accept_nickname) + ' 加入 ' + item.dry_team_name"
				:badgeText="item.dry_add_time">
					<template v-slot:header>
						<view class="slot-box">
							<image class="slot-image" :src="item.dry_invitor == user.id ? item.dry_accept_avatar : item.dry_invite_avatar" mode="widthFix"></image>
						</view>
					</template>
					<template v-slot:footer>
						<view v-if="item.dry_invite_status == 0">
							<view v-if="item.dry_acceptor == user.id">
								<uni-tag text="同意" type="primary" @click="acceptOrRefused(item,'accept')"></uni-tag>
								<uni-tag text="拒绝" type="primary" @click="acceptOrRefused(item,'refused')"></uni-tag>
							</view>
							<view v-else>
								<uni-tag text="未处理" type="default" disabled></uni-tag>
							</view>
						</view>
						<view v-if="item.dry_invite_status == 1">
							<uni-tag text="已接受" type="default" disabled></uni-tag>
						</view>
						<view v-if="item.dry_invite_status == 2">
							<uni-tag text="已拒绝" type="default" disabled></uni-tag>
						</view>
					</template>
				</uni-list-item>
			</uni-list>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: [],
				inviteList: [],
			}
		},
		onShareAppMessage(res) {
		    if (res.from === 'button') {// 来自页面内分享按钮
		      console.log(res.target)
		    }
		    return {
		      title: '大家都在用这款小工具管理项目，快来试试吧！！！',
		      path: '/pages/index/index'
		    }
		},
		onShow(){
			this.user = this.$dry.get("user");
			this.getInviteList();
		},
		methods: {
			acceptOrRefused(item,status){
				var that = this;
				var token = this.$dry.getToken();
				var data = item;
				if(status == 'accept'){
					data['dry_invite_status'] = 1;
				}
				if(status == 'refused'){
					data['dry_invite_status'] = 2;
				}
				if(token){
				    data = this.$request.run("editInvite",data,token);
				    uni.request({
				    	url: data[0].url,
				    	method: data[0].method,
				    	header: data[0].header,
				    	data: data[0].data,
				    	success: data => {
							if(status == 'accept'){
								var employeeData = [];
								employeeData['dry_team'] = item.dry_team;
								employeeData['dry_employee'] = item.dry_acceptor;
								employeeData['dry_note'] = item.dry_accept_nickname;
								employeeData['dry_is_auditor'] = 0;
								employeeData['dry_is_reimbursement'] = 0;
								employeeData['dry_employee_avatar'] = item.dry_accept_avatar;
								employeeData = this.$request.run("addEmployee",employeeData,token);
								uni.request({
									url: employeeData[0].url,
									method: employeeData[0].method,
									header: employeeData[0].header,
									data: employeeData[0].data,
									success: data => {
										uni.showToast({
											title: "已接受邀请"
										});
									}
								});
							}
							if(status == 'refused'){
								uni.showToast({
									title: "拒绝邀请"
								})
							}
							that.getInviteList();
						}
					})
				}
			},
			getInviteList(){
				var token = this.$dry.getToken();
				var data = [];
				if(token){
					data = this.$request.run("listInvite",token);
					uni.request({
						url: data.url,
						method: data.method,
						header: data.header,
						data: {},
						success: data => {
							if(data.statusCode === 200) {
								this.inviteList = data.data.data.list;
							}
						}
					})
				}
			}
		}
	}
</script>

<style>
	.slot-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
	.slot-image {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		margin-right: 10px;
		width: 30px;
		height: 30px;
	}
	
	.slot-text {
		flex: 1;
		font-size: 14px;
		margin-right: 10px;
	}
</style>
